from django.core.management.base import BaseCommand
from blog.models import Article, Tag, Category, Carousel
from blog_system.models import AD
from django.contrib.auth import get_user_model
from django.core.exceptions import ObjectDoesNotExist
import datetime


class Command(BaseCommand):
    help = 'create test date'

    def handle(self, * args, ** options):
        print('创建文章中........')
        self.make_article(15, 0)
        print('创建专题中........')
        self.make_article(10, 2)
        print('创建公告中.......')
        self.make_article(5, 3)
        print('创建轮播图中........')
        self.make_carousel()
        print("创建广告.......")
        self.make_ad()
        print('创建完毕')

    def make_article(self, num, type):
        """
        用于生成文章
        :param num: 生成文章的数量
        :param type: 生成文章的类型,整数型
        : returns: 不返回结果
        """

        # 获取或者创建测试用户
        user = get_user_model().objects.get_or_create(
                                                                                                            email='test@test.com', 
                                                                                                            username='测试用户',
                                                                                                            password='123456'
                                                                                                        )[0]
        # 获取或者创建测试父类与子类
        parent = Category.objects.get_or_create(name="测试父类", parent=None)[0]
        category = Category.objects.get_or_create(name='测试子类', parent=parent)[0]
        category.save()

        # 获取或创建测试标签
        tag1 = Tag.objects.get_or_create(name="测试标签1")[0]
        tag1.save()

        # 创建文章
        content = "     \
        # this test        \
        ## test              \
        + test                 \
            + test              \
            1. 456              \
            2. 123  \
        ```bash         \
        echo 'hello world'    \
        ```     \
        "
        for i in range(1, num+1):
            article = Article.objects.get_or_create(category = category,
                                                                                            title=f'title{str(i)}',
                                                                                            img='images/default.jpg',
                                                                                            content=content,
                                                                                            author=user,
                                                                                            summary=f'this is summary about article{str(i)}'
                                                                                            )[0]
            tag2 = Tag.objects.get_or_create(name="测试标签2")[0]
            tag2.save()
            article.tag.add(tag1)
            article.tag.add(tag2)
            article.save()

    def make_carousel(self):
        for i in range(1, 5):
            article = Article.objects.get(id=i)
            carousel = Carousel.objects.get_or_create(article=article, img=f'carousel/{str(i)}.jpg')[0]
            carousel.save()

    def make_ad(self):
        for i in range(1, 3):
            ad = AD.objects.get_or_create(url="https://www.baidu.com", img=f"ad/h{str(i)}.jpg", desc="百度一下我也不知道")[0]
            ad.save()
